import { createRouter, createWebHistory } from 'vue-router'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/LoginView.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/login/RegisterView.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/user',
    component: () => import('../layout/UserLayout.vue'),
    redirect: '/user/home',
    meta: { requiresAuth: true, role: 0 },
    children: [
      {
        path: 'home',
        name: 'UserHome',
        component: () => import('../views/dashboard/UserHome.vue'),
        meta: { title: '用户首页', requiresAuth: true, role: 0 }
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/user/UserProfile.vue'),
        meta: { title: '个人信息', requiresAuth: true, role: 0 }
      },
      {
        path: 'announcement',
        name: 'UserAnnouncement',
        component: () => import('../views/user/Announcement.vue'),
        meta: { title: '公告信息', requiresAuth: true, role: 0 }
      },
      {
        path: 'parking',
        name: 'UserParking',
        component: () => import('../views/parking/ParkingView.vue'),
        meta: { title: '停车场', requiresAuth: true, role: 0 }
      },
      {
        path: 'reservation',
        name: 'UserReservation',
        component: () => import('../views/parking/ReservationView.vue'),
        meta: { title: '预约车位', requiresAuth: true, role: 0 }
      },
      {
        path: 'record',
        name: 'UserRecord',
        component: () => import('../views/parking/ParkingRecord.vue'),
        meta: { title: '出入场信息', requiresAuth: true, role: 0 }
      },
      {
        path: 'payment',
        name: 'UserPayment',
        component: () => import('../views/payment/PaymentView.vue'),
        meta: { title: '智付停车', requiresAuth: true, role: 0 }
      }
    ]
  },
  {
    path: '/admin',
    component: () => import('../layout/AdminLayout.vue'),
    redirect: '/admin/home',
    meta: { requiresAuth: true, role: 1 },
    children: [
      {
        path: 'home',
        name: 'AdminHome',
        component: () => import('../views/admin/AdminHome.vue'),
        meta: { title: '管理首页', requiresAuth: true, role: 1 }
      },
      {
        path: 'user',
        name: 'AdminUser',
        component: () => import('../views/admin/UserManagement.vue'),
        meta: { title: '用户管理', requiresAuth: true, role: 1 }
      },
      {
        path: 'announcement',
        name: 'AdminAnnouncement',
        component: () => import('../views/admin/AnnouncementManagement.vue'),
        meta: { title: '公告管理', requiresAuth: true, role: 1 }
      },
      {
        path: 'parking',
        name: 'AdminParking',
        component: () => import('../views/admin/ParkingManagement.vue'),
        meta: { title: '停车场管理', requiresAuth: true, role: 1 }
      },
      {
        path: 'reservation',
        name: 'AdminReservation',
        component: () => import('../views/admin/ReservationManagement.vue'),
        meta: { title: '预约管理', requiresAuth: true, role: 1 }
      },
      {
        path: 'record',
        name: 'AdminRecord',
        component: () => import('../views/admin/RecordManagement.vue'),
        meta: { title: '出入场信息', requiresAuth: true, role: 1 }
      },
      {
        path: 'system',
        name: 'AdminSystem',
        component: () => import('../views/admin/SystemManagement.vue'),
        meta: { title: '系统管理', requiresAuth: true, role: 1 }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: { title: '404 Not Found', requiresAuth: false }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || '小区智慧停车场管理系统'
  
  // 获取存储的用户信息
  const userInfo = localStorage.getItem('userInfo')
  const userObj = userInfo ? JSON.parse(userInfo) : null
  
  // 如果页面需要认证
  if (to.meta.requiresAuth) {
    // 如果没有登录，则重定向到登录页面
    if (!userObj) {
      next({ name: 'Login' })
    } 
    // 如果角色不匹配，则重定向到对应角色的首页
    else if (to.meta.role !== undefined && to.meta.role !== userObj.role) {
      if (userObj.role === 0) {
        next({ name: 'UserHome' })
      } else {
        next({ name: 'AdminHome' })
      }
    } 
    // 否则允许访问
    else {
      next()
    }
  } 
  // 如果页面不需要认证，则直接允许访问
  else {
    next()
  }
})

export default router 